i-> location name
[3]->value at location
65525->location number (address)
Program to print address of variable :
void main()
{
int i=3;
printf(""\n address of i=%u",&i);
printf("\n value of i=%d",i);
}
Output : address of i=65525
value of i=3
In above program and the address of operator .The expression and i returns the address of variable which is in this case happens to be 65525 since 65525 represent an address there is no question of a sign being associated with it hence it is printed out %u which is a format specifier for printing an unsigned integer . we have been using the '&' operator all the time in the scanf()statement.
Pointer is a variable which stores the address of another variable rather than value.
We can not use pointer without declaring it.
It is declared as
int *j;
int i=3;
int *j;
j=&i;
j as an pointer here which is pointing to the address of variable i.
datatype of i and j should be the same.
Program-
void main()
{
int i=3;
int *j;
j=&i;
printf("\n address of i=%u",&i);
printf("\n address of j=%u",&j);
printf("\n value of i=%d",i);
printf("\n value of j=%u",j);
printf("\n value of i=%d",*(&i));
}
Output :
address of i=65524
address of j=65522
value of i=3
value of j=65524
value of i=3There are two types of functions call:
void swap(int,int);
void main()
{
int a=10,b=20;
swap(a,b);
printf("before swapping"); //before swapping
printf("\n a=%d b=%d"a,b);
}
void swap(int x,int y)
{
int t;
t=x;
x=y; //swapping of number using third variable
y=t;
printf("after swapping");
printf("\n x=%d y=%d",x,y);
}
Output :
before swapping
a=10 b=20
after swapping
x=20 y=10
void swap(int *x,int *y)
void main()
{
int a=10,b=20;
swap(&a,&b);
printf("before swapping");
printf("\n a=%d b=%d"a,b);
}
void swap(int *x,int *y)
{
int t;
t= *x;
*x=*y;
*y=t;
printf("after swapping");
printf("\n x=%d y=%d",x,y);
}
Output :
before swapping
a=10 b=20
after swapping
x=20 y=10